package com.wayne.blog.controller;

import com.wayne.blog.common.Ret;
import com.wayne.blog.dto.CommentDTO;
import com.wayne.blog.entity.Comment;
import com.wayne.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论控制器
 */
@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping
    public Ret<?> addComment(@RequestBody CommentDTO commentDTO) {
        try {
            commentService.addComment(commentDTO);
            return Ret.success();
        } catch (Exception e) {
            return Ret.error(e.getMessage());
        }
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{id}")
    public Ret<?> deleteComment(@PathVariable Long id) {
        try {
            commentService.deleteComment(id);
            return Ret.success();
        } catch (Exception e) {
            return Ret.error(e.getMessage());
        }
    }

    /**
     * 获取文章评论列表
     */
    @GetMapping("/article/{articleId}")
    public Ret<?> getArticleComments(@PathVariable Long articleId) {
        try {
            List<Comment> comments = commentService.getArticleComments(articleId);
            return Ret.success(comments);
        } catch (Exception e) {
            return Ret.error(e.getMessage());
        }
    }

    /**
     * 点赞评论
     */
    @PostMapping("/{id}/like")
    public Ret<?> likeComment(@PathVariable Long id) {
        try {
            commentService.likeComment(id);
            return Ret.success();
        } catch (Exception e) {
            return Ret.error(e.getMessage());
        }
    }

    /**
     * 取消点赞评论
     */
    @PostMapping("/{id}/unlike")
    public Ret<?> unlikeComment(@PathVariable Long id) {
        try {
            commentService.unlikeComment(id);
            return Ret.success();
        } catch (Exception e) {
            return Ret.error(e.getMessage());
        }
    }
} 